package com.tencent.wxpay.demo.controller;

import com.tencent.wxpay.demo.service.DownloadBillService;
import com.tencent.wxpay.protocol.request.DownloadBillReqData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 关闭订单服务控制层
 */
@Controller
@RequestMapping("downloadBill")
public class DownloadBillController {

    @Autowired
    private DownloadBillService downloadBillService;

    /**
     * 调用下载对账单接口
     * @param billDate 账单日期 格式：yyyyMMdd
     * @param billType 账单类型 取值：ALL, SUCCESS, REFUND
     * @return
     */
    @RequestMapping("doDownloadBill")
    @ResponseBody
    public Map<String, Object> doUnifiedOrder(String billDate, String billType) {
        Map<String,Object> map = new HashMap();
        if(StringUtils.isNotBlank(billDate)) {
            boolean isExit = false;
            for( DownloadBillReqData.BillType bt : DownloadBillReqData.BillType.values()){
                if(bt.toString().equals(billType.toUpperCase())){
                    isExit = true;
                    break;
                }
            }
            if(isExit) {
                Object result = downloadBillService.doDownloadBill(billDate,DownloadBillReqData.BillType.valueOf(billType.toUpperCase()));
                if (null != result) {
                    map.put("result",result);
                    map.put("status", "success");
                } else {
                    map.put("status","error");
                    map.put("msg", "下载对账单失败，详细信息请查看服务端日志输出");
                }
            } else {
                map.put("status","error");
                map.put("msg", "下载对账单失败，账单类型不能为空");
            }
        } else {
            map.put("status","error");
            map.put("msg", "下载对账单失败，账单日期不能为空");
        }
        return map;
    }
}
